home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / TextField.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  31.1 KB  |  645 lines

  1. /*
  2.  *
  3.  * 31    1/30/97 1:03p Cthrons
  4.  * Remove debug println statement.
  5.  *
  6.  * 30    1/24/97 4:27p Tjones
  7.  * Removed 'throws SQLException' from setbinding method
  8.  *
  9.  * 29    1/17/97 3:14p Tjones
  10.  * Modified setBinding method to throw SQLException
  11.  *
  12.  * 28    1/17/97 2:53p Tjones
  13.  * Fixed bug with setEditable
  14.  *
  15.  * 27    12/19/96 1:42p Tjones
  16.  * Bug fix with the setTreatBlankAs method
  17.  *
  18.  * 26    12/16/96 9:41a Tjones
  19.  * Change parameter list to accomodate new setValueFrom String.
  20.  * Add setTreatBlankAs method.
  21.  *
  22.  * 25    12/08/96 4:31p Tjones
  23.  * Make notifySetData throw SQLException and use binder.getStringValue
  24.  * method.
  25.  *
  26.  * 24    12/07/96 6:18p Tjones
  27.  * Changed return type of notifySetData to boolean.
  28.  *
  29.  * 23    12/06/96 2:13p Tjones
  30.  * Minor commenting additions
  31.  *
  32.  * 22    12/06/96 7:30a Tjones
  33.  * Fixed bug with clearing fields for new record.
  34.  *
  35.  * 21    12/05/96 9:03p Cthrons
  36.  * Check for valid current record state before calling isReadable.
  37.  *
  38.  * 20    12/05/96 3:03p Cthrons
  39.  * Took out some footprint statements.
  40.  *
  41.  * 19    12/05/96 2:43p Tjones
  42.  * Bug fix with clearing fields for new record.
  43.  *
  44.  * 18    12/05/96 12:49p Cthrons
  45.  * A whole bunch of changes...
  46.  *
  47.  * 17    12/04/96 4:15p Tjones
  48.  * Implement use of set/getAsciiStream for LONGVARCHAR data type
  49.  *
  50.  * 16    11/27/96 5:24p Tjones
  51.  *
  52.  * 15    11/27/96 4:30p Tjones
  53.  *
  54.  * 14    11/27/96 8:40a Tjones
  55.  *
  56.  * 13    11/26/96 4:49p Tjones
  57.  *
  58.  * 12    11/26/96 4:33p Tjones
  59.  * Added notifySetData()
  60.  *
  61.  * 11    11/25/96 2:40p Tjones
  62.  *
  63.  * 10    11/25/96 1:42p Tjones
  64.  *
  65.  *
  66.  * 9     11/25/96 1:26p Tjones
  67.  * Introduced dynamic editing method
  68.  *
  69.  * 8     11/21/96 3:54p Tjones
  70.  *
  71.  * 7     11/21/96 3:52p Tjones
  72.  * Reinstituted the setBinding method
  73.  *
  74.  * 6     11/14/96 8:22p Cthrons
  75.  * Put in safety code to make sure binder is readable, writable, etc...
  76.  *
  77.  * 5     11/01/96 1:42p Cthrons
  78.  * Process key input.
  79.  *
  80.  * 4     10/25/96 11:14p Kbradle
  81.  * Remove internal setProjBinding routine (no longer needed)
  82.  * Add calls so that the controls get the first record of data during
  83.  * init().
  84.  *
  85.  * 3     10/15/96 7:27p Kbradle
  86.  * Implement lostFocus and gotFocus which compares the values and only
  87.  * sends data to scale if it changed.
  88.  *
  89.  * 2     10/11/96 7:01p Kbradle
  90.  * change LostFocus to lostFocus (case typo)
  91.  *
  92.  * 1     10/03/96 11:59p Cthrons
  93. */
  94.  
  95. package symantec.itools.db.awt;
  96.  
  97. // import java.awt.*;
  98. import java.util.*;
  99. import symjava.sql.*;
  100. import java.lang.*;
  101. import symantec.itools.db.net.*;
  102. import symantec.itools.db.pro.*;
  103.  
  104.     //*************************************************************************//
  105.     //                                                                         //
  106.     //    CLASS: TextField                                                     //
  107.     //                                                                         //
  108.     //  PURPOSE: A TextField object is a single-line area that displays text.  //
  109.     //              It can be bound to a projection in a database.             //
  110.     //              It can be set to allow editing or read-only modes.         //
  111.     //              dbAware TextField class                                    //
  112.     //                                                                         //
  113.     // PARAMETERS:                                                             //
  114.     //                                                                         //
  115.     //  RETURNS:                                                               //
  116.     //                                                                         //
  117.     // COMMENTS:                                                               //
  118.     //                                                                         //
  119.     //*************************************************************************//
  120.  
  121. public class TextField extends java.awt.TextField implements ProjLink
  122. {
  123.     private ProjBinder m_ProjBinder; // Object used to interact with server
  124.  
  125.     private boolean m_DynamicUpdate = false;
  126.     private boolean m_DynamicUpdateOverride = true;
  127.     private boolean m_BinderDetermines = true;
  128.     private String  m_BinderData;    // Keep in synch with database
  129.     private String  m_ScreenData;    // Keep in synch with screen text
  130.     private int     m_treatBlankAs = 0;
  131.  
  132.     //*************************************************************************//
  133.     //                                                                         //
  134.     // FUNCTION: TextField                                                     //
  135.     //                                                                         //
  136.     //  PURPOSE: Default constructor                                           //
  137.     //                                                                         //
  138.     // PARAMETERS:                                                             //
  139.     //                                                                         //
  140.     //  RETURNS:                                                               //
  141.     //                                                                         //
  142.     // COMMENTS:                                                               //
  143.     //                                                                         //
  144.     //*************************************************************************//
  145.  
  146.     public TextField()
  147.     {
  148.         super();
  149.         m_BinderData = new String();
  150.         m_ScreenData = new String();
  151.     }
  152.  
  153.     //*************************************************************************//
  154.     //                                                                         //
  155.     // FUNCTION: TextField                                                     //
  156.     //                                                                         //
  157.     //  PURPOSE:                                                               //
  158.     //  * Constructs a new TextField with the specified number of columns.     //
  159.     //                                                                         //
  160.     // PARAMETERS:                                                             //
  161.     //  * @param cols the number of columns                                    //
  162.     //                                                                         //
  163.     //  RETURNS:                                                               //
  164.     //                                                                         //
  165.     // COMMENTS:                                                               //
  166.     //                                                                         //
  167.     //*************************************************************************//
  168.  
  169.     public TextField(int cols)
  170.     {
  171.         super(cols);
  172.         m_ScreenData = new String();
  173.         m_BinderData = new String();
  174.     }
  175.  
  176.     //*************************************************************************//
  177.     //                                                                         //
  178.     // FUNCTION: TextField                                                     //
  179.     //                                                                         //
  180.     //  PURPOSE:                                                               //
  181.     //  * Constructs a new TextField with the specified text displayed.        //
  182.     //                                                                         //
  183.     // PARAMETERS:                                                             //
  184.     //  * @param text the text to be displayed                                 //
  185.     //                                                                         //
  186.     //  RETURNS:                                                               //
  187.     //                                                                         //
  188.     // COMMENTS:                                                               //
  189.     //                                                                         //
  190.     //*************************************************************************//
  191.  
  192.     public TextField(String text)
  193.     {
  194.         super(text);
  195.         m_ScreenData = new String();
  196.         m_BinderData = new String();
  197.     }
  198.  
  199.     //*************************************************************************//
  200.     //                                                                         //
  201.     // FUNCTION: TextField                                                     //
  202.     //                                                                         //
  203.     //  PURPOSE:                                                               //
  204.     //  * Constructs a new TextField with the specified text and number of columns.
  205.     //                                                                         //
  206.     // PARAMETERS:                                                             //
  207.     //  * @param text the text to be displayed                                 //
  208.     //  * @param cols the number of columns                                    //
  209.     //                                                                         //
  210.     //  RETURNS:                                                               //
  211.     //                                                                         //
  212.     // COMMENTS:                                                               //
  213.     //                                                                         //
  214.     //*************************************************************************//
  215.  
  216.     public TextField(String text, int cols)
  217.     {
  218.         super(text, cols);
  219.         m_ScreenData = new String();
  220.         m_BinderData = new String();
  221.     }
  222.  
  223.     //*************************************************************************//
  224.     //                                                                         //
  225.     // FUNCTION: init                                                          //
  226.     //                                                                         //
  227.     //  PURPOSE:                                                               //
  228.     //                                                                         //
  229.     // PARAMETERS:                                                             //
  230.     //  @param binder The ProjBinder binder object which this component can use//
  231.     //                to change data at the database server.                   //
  232.     //                                                                         //
  233.     //  RETURNS:                                                               //
  234.     //                                                                         //
  235.     // COMMENTS:                                                               //
  236.     //  Implement the ProjLink interface function, init().                     //
  237.     //  This function is called when the control is first bound to data.       //
  238.     //                                                                         //
  239.     //*************************************************************************//
  240.  
  241.     public void init (ProjBinder binder)
  242.     {
  243.         m_ProjBinder = binder;
  244.         setEditable(m_ProjBinder);
  245.     }
  246.  
  247.     //*************************************************************************//
  248.     //                                                                         //
  249.     // FUNCTION: setBinding                                                    //
  250.     //                                                                         //
  251.     //  PURPOSE:                                                               //
  252.     //                                                                         //
  253.     // PARAMETERS:                                                             //
  254.     //                                                                         //
  255.     //  RETURNS:                                                               //
  256.     //                                                                         //
  257.     // COMMENTS:                                                               //
  258.     //                                                                         //
  259.     //*************************************************************************//
  260.  
  261.     public void setBinding(RelationView relView, String projection)
  262.     {
  263.         int projectionNumber = 0;
  264.         int columnType = 0;
  265.  
  266.         try
  267.         {
  268.             RelationViewMetaData rvmd = relView.getMetaData();
  269.             projectionNumber = relView.findProjByName(projection);
  270.             columnType = rvmd.getColumnType(projectionNumber);
  271.             if ( (columnType == Types.DATE) ||
  272.                  (columnType == Types.TIME) ||
  273.                  (columnType == Types.TIMESTAMP) )
  274.             {
  275.                 m_DynamicUpdateOverride = false;
  276.             }
  277.             relView.bindProj(projectionNumber, this);
  278.         }
  279.         catch (SQLException Ex)
  280.         {
  281.             raiseException(
  282.                 "SQLException from setBinding: "
  283.                 + Ex.getMessage());
  284.         }
  285.     }
  286.  
  287.     //*************************************************************************//
  288.     //                                                                         //
  289.     // FUNCTION: setDynamicUpdate                                              //
  290.     //                                                                         //
  291.     //  PURPOSE:                                                               //
  292.     //                                                                         //
  293.     // PARAMETERS:                                                             //
  294.     //                                                                         //
  295.     //  RETURNS:                                                               //
  296.     //                                                                         //
  297.     // COMMENTS:                                                               //
  298.     //                                                                         //
  299.     //*************************************************************************//
  300.  
  301.     public void setDynamicUpdate(boolean update)
  302.     {
  303.         m_DynamicUpdate = update;
  304.     }
  305.  
  306.     public void setTreatBlankAs(String blank)
  307.     {
  308.         if (new String(blank).toUpperCase().equals("DEFAULT"))
  309.         {
  310.             m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
  311.         }
  312.             else if (new String(blank).toUpperCase().equals("NULL"))
  313.             {
  314.                 m_treatBlankAs = RelationView.SETBLANKTONULL;
  315.             }
  316.             else if (new String(blank).toUpperCase().equals("BLANK"))
  317.                 {
  318.                     m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
  319.                 }
  320.     }
  321.  
  322.     //*************************************************************************//
  323.     //                                                                         //
  324.     // FUNCTION: notifyDataChange                                              //
  325.     //                                                                         //
  326.     //  PURPOSE:                                                               //
  327.     //    Implement the ProjLink interface function, notifyDataChange().       //
  328.     //    This function is called when the projection's data changes at        //
  329.     //    the database.                                                        //
  330.     //                                                                         //
  331.     // PARAMETERS:                                                             //
  332.     //  @param binder The ProjBinder binder object which this component can use//
  333.     //                to get or change data at the database server.            //
  334.     //                                                                         //
  335.     //  RETURNS:                                                               //
  336.     //                                                                         //
  337.     // COMMENTS:                                                               //
  338.     //                                                                         //
  339.     //  Step 1: set m_ProjBinder equal to binder object                        //
  340.     //  Step 2: synchronize m_BinderData with binder object                    //
  341.     //  Step 3: synchronize m_ScreenData with current screen                   //
  342.     //  Step 4: if the two are different then                                  //
  343.     //       - set m_ScreenData equal to m_BinderData to avoid recursion       //
  344.     //       - call setText to update screen                                   //
  345.     //  Step 5: enable or disable editing based on binder                      //
  346.     //                                                                         //
  347.     //*************************************************************************//
  348.  
  349.     public void notifyDataChange(ProjBinder binder)
  350.     {
  351.         m_BinderData = "";
  352.         // STEP 1
  353.         if (binder == null)
  354.         {
  355.             // Raise an exception..
  356.             return;
  357.         }
  358.         m_ProjBinder = binder;
  359.  
  360.         // STEP 2
  361.         try
  362.         {
  363.             if (binder.getRelationView().getCurrentRecordState()
  364.                 != Record.RECSTATE_INVALID)
  365.             {
  366.                 if (binder.isReadable() && !binder.isNull())
  367.                 {
  368.                     m_BinderData = binder.getStringValue();
  369.                 }
  370.             }
  371.         }
  372.         catch (SQLException Ex)
  373.         {
  374.             raiseException(
  375.                 "SQLException from TextField.notifyDataChange.getString: "
  376.                 + Ex.getMessage());
  377.         }
  378.         catch (java.io.IOException Ex)
  379.         {
  380.             raiseException(
  381.                 "IOException from TextField.notifyDataChange.getString: "
  382.                 + Ex.getMessage());
  383.         }
  384.         if (m_BinderData == null)
  385.         {
  386.             m_BinderData = new String();
  387.         }
  388.  
  389.         // STEP 3
  390.         m_ScreenData = getText();
  391.  
  392.         // STEP 4
  393.         if (!m_BinderData.equals(m_ScreenData))
  394.         {
  395.             m_ScreenData = m_BinderData;
  396.             setText(m_ScreenData);
  397.         }
  398.  
  399.         // STEP 5
  400.         if (m_BinderDetermines)
  401.         {
  402.             setEditable(m_ProjBinder);
  403.         }
  404.     }
  405.  
  406.     //*************************************************************************//
  407.     //                                                                         //
  408.     // FUNCTION: lostFocus                                                     //
  409.     //                                                                         //
  410.     //  PURPOSE: Intercept the event to send data to the binder                //
  411.     //                                                                         //
  412.     // PARAMETERS:                                                             //
  413.     //      @param evt the lost focus event being caught                       //
  414.     //      @param what the object that is losing focus                        //
  415.     //                                                                         //
  416.     //  RETURNS:                                                               //
  417.     //                                                                         //
  418.     // COMMENTS:                                                               //
  419.     //      Call notifySetData since that is the standard way of handling      //
  420.     //      any event that should cause us to send data to the binder.         //
  421.     //                                                                         //
  422.     //*************************************************************************//
  423.  
  424.     public boolean lostFocus(java.awt.Event evt, Object what)
  425.     {
  426.         try
  427.         {
  428.             notifySetData(m_ProjBinder);
  429.         }
  430.         catch (SQLException Ex)
  431.         {
  432.             raiseException(
  433.                 "SQLException from TextField.notifySetData: "
  434.                 + Ex.getMessage());
  435.         }
  436.         return super.lostFocus(evt, what);
  437.     }
  438.  
  439.     //*************************************************************************//
  440.     //                                                                         //
  441.     // FUNCTION: notifyInputChange                                             //
  442.     //                                                                         //
  443.     //  PURPOSE: Handle any event that causes us to send data to binder object //
  444.     //                                                                         //
  445.     //  PARAMETERS:                                                            //
  446.     //                                                                         //
  447.     //  RETURNS:                                                               //
  448.     //                                                                         //
  449.     // COMMENTS:                                                               //
  450.     //                                                                         //
  451.     //                                                                         //
  452.     //  Step 1: synchronize m_ScreenData with current screen                   //
  453.     //  Step 2: if m_ScreenData differs from m_BinderData then...              //
  454.     //       - set m_BinderData equal to m_ScreenData to avoid recursion       //
  455.     //       - call m_Binder.setString to update binder                        //
  456.     //                                                                         //
  457.     //*************************************************************************//
  458.  
  459.     boolean notifyInputChanged(String input)
  460.     {
  461.         // STEP 1
  462.         if (!m_ScreenData.equals(input))
  463.         {
  464.             m_ScreenData = input;
  465.         }
  466.  
  467.         // STEP 2
  468.         try{
  469.         if (!m_BinderData.equals(m_ScreenData)
  470. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_INVALID)
  471. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_DELETED)
  472. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_DB_DELETED))
  473.         {
  474.             m_BinderData = m_ScreenData;
  475.             try
  476.             {
  477.                 m_ProjBinder.setValueFromString(m_ScreenData, 0, m_treatBlankAs);
  478.             }
  479.  
  480.             catch (java.io.IOException Ex)
  481.             {
  482.                 raiseException(
  483.                     "IOException from TextField.notifyInputChange: "
  484.                     + Ex.getMessage());
  485.                 return false;
  486.             }
  487.         }
  488.         }catch (SQLException Ex)
  489.             {
  490.                 raiseException(
  491.                     "SQLException from TextField.notifyInputChange: "
  492.                     + Ex.getMessage());
  493.                 return false;
  494.             }
  495.         return true;
  496.     }
  497.  
  498.     //*************************************************************************//
  499.     //                                                                         //
  500.     // FUNCTION: notifySetData                                                 //
  501.     //                                                                         //
  502.     //  PURPOSE: Send data to binder                                           //
  503.     //                                                                         //
  504.     //  PARAMETERS:                                                            //
  505.     //  @param binder The ProjBinder binder object which this component can use//
  506.     //                to change data at the database server.                   //
  507.     //                                                                         //
  508.     //  RETURNS:                                                               //
  509.     //                                                                         //
  510.     // COMMENTS:                                                               //
  511.     //                                                                         //
  512.     //                                                                         //
  513.     //*************************************************************************//
  514.  
  515.     public boolean notifySetData(ProjBinder binder) throws SQLException
  516.     {
  517.         return ( notifyInputChanged(getText()) );
  518.     }
  519.  
  520.     //*************************************************************************//
  521.     //                                                                         //
  522.     // FUNCTION: handleEvent                                                   //
  523.     //                                                                         //
  524.     //  PURPOSE: Respond to any event we want to handle ourselves.             //
  525.     //                                                                         //
  526.     //  PARAMETERS:                                                            //
  527.     //                                                                         //
  528.     //  RETURNS:                                                               //
  529.     //                                                                         //
  530.     // COMMENTS:                                                               //
  531.     // * Handles the event. Returns true if the event is handled and           //
  532.     // * should not be passed to the parent of this component. The default     //
  533.     // * event handler calls some helper methods to make life easier           //
  534.     // * on the programmer.                                                    //
  535.     // * @param evt the event                                                  //
  536.     // * @;see #mouseEnter                                                     //
  537.     // * @see #mouseExit                                                       //
  538.     // * @see #mouseMove                                                       //
  539.     // * @see #mouseDown                                                       //
  540.     // * @see #mouseDrag                                                       //
  541.     // * @see #mouseUp                                                         //
  542.     // * @see #keyDown                                                         //
  543.     // * @see #action                                                          //
  544.     //                                                                         //
  545.     //*************************************************************************//
  546.  
  547.     public boolean handleEvent(java.awt.Event evt) {
  548.         // debugPrint(evt.toString());
  549.         if (m_DynamicUpdate && m_DynamicUpdateOverride)
  550.         {
  551.             try
  552.             {
  553.                 notifySetData(m_ProjBinder);
  554.             }
  555.             catch (SQLException Ex)
  556.             {
  557.                 raiseException(
  558.                     "SQLException from TextField.notifySetData: "
  559.                     + Ex.getMessage());
  560.             }
  561.         }
  562.         return super.handleEvent(evt);
  563.     }
  564.  
  565.     //*************************************************************************//
  566.     //                                                                         //
  567.     // FUNCTION: setEditable                                                   //
  568.     //                                                                         //
  569.     //  PURPOSE: Enable or disable input                                       //
  570.     //                                                                         //
  571.     //  PARAMETERS:                                                            //
  572.     //  @param binder The ProjBinder binder object which this component can use//
  573.     //                to change data at the database server.                   //
  574.     //                                                                         //
  575.     //  RETURNS:                                                               //
  576.     //                                                                         //
  577.     // COMMENTS:                                                               //
  578.     //                                                                         //
  579.     //*************************************************************************//
  580.  
  581.     void setEditable(ProjBinder binder)
  582.     {
  583.         //Disable input if binder is not writeable
  584.         boolean isWritable = false;
  585.  
  586.         try
  587.         {
  588.             if (binder != null)
  589.             {
  590.                 RelationView rv = binder.getRelationView();
  591.                 if (rv != null && (rv.getCurrentRecordState()
  592.                                          != Record.RECSTATE_INVALID))
  593.                 {
  594.                     isWritable = binder.isWritable();
  595.                 }
  596.             }
  597.         }
  598.         catch (SQLException Ex)
  599.         {
  600.             raiseException(
  601.                 "SQLException from setEditable: "
  602.                 + Ex.getMessage());
  603.         }
  604.         super.setEditable(isWritable);
  605.     }
  606.  
  607.     //*************************************************************************//
  608.     //                                                                         //
  609.     // FUNCTION: setEditable                                                   //
  610.     //                                                                         //
  611.     //  PURPOSE: Set editable attribute                                        //
  612.     //                                                                         //
  613.     //  PARAMETERS:                                                            //
  614.     //                                                                         //
  615.     //  RETURNS:                                                               //
  616.     //                                                                         //
  617.     // COMMENTS:                                                               //
  618.     //                                                                         //
  619.     //*************************************************************************//
  620.  
  621.     public void setEditable(boolean value)
  622.     {
  623.         m_BinderDetermines = value;
  624.         super.setEditable(value);
  625.     }
  626.  
  627.     //*************************************************************************//
  628.     //                                                                         //
  629.     // FUNCTION: raiseException                                                //
  630.     //                                                                         //
  631.     //  PURPOSE:                                                               //
  632.     //                                                                         //
  633.     //  PARAMETERS:                                                            //
  634.     //                                                                         //
  635.     //  RETURNS:                                                               //
  636.     //                                                                         //
  637.     // COMMENTS:                                                               //
  638.     //                                                                         //
  639.     //*************************************************************************//
  640.  
  641.     void raiseException(String text)
  642.     {
  643.         System.out.println(text);
  644.     }
  645. }